//
//  BGFeatureViewController.swift
//  Baga
//
//  Created by 飞王 on 16/12/27.
//  Copyright © 2016年 飞王. All rights reserved.
//

import UIKit

class BGFeatureViewController: UIViewController, UIScrollViewDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews();
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    // MARK: - ui搭建
    private func setupViews() {
        view.addSubview(scrollView);
        
        let count = 4;
        
        for i in 0..<count {
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i)"));
            
            imageView.frame = CGRect(x: (CGFloat(i) * SCREENW), y: 0, width: SCREENW, height: SCREENH);
            scrollView.addSubview(imageView);
            
            if i == count - 1 {
                setupLastPage(imageView: imageView);
            }
            
        }
        
        // 设置内容大小，可以滚动
        scrollView.contentSize = CGSize(width: CGFloat(count) * SCREENW, height: 0);
        
    }
    
    
    private func setupLastPage(imageView: UIImageView) {
        imageView.isUserInteractionEnabled = true;
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(BGFeatureViewController.goToMainController));
        
//        let tapGesture = UITapGestureRecognizer(target: self, action: #selector("goToMainController:"));
        tapGesture.numberOfTapsRequired = 1;
        imageView.addGestureRecognizer(tapGesture);
        
    }
    
    
    // MARK: - UIScrollerViewDelegate
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
    }
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        
    }
    
    // MARK: - 点击进入主页面
    
    @objc private func goToMainController() {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: SWITCHROOTCONTROLLER), object: nil);
        printLog(message: "go to main controller");
    }

    // MARK: - 懒加载
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView();
        scrollView.backgroundColor = UIColor.purple;
        scrollView.isPagingEnabled = true;
        scrollView.showsHorizontalScrollIndicator = false;
        scrollView.delegate = self;
        scrollView.frame = UIScreen.main.bounds;
        scrollView.bounces = false;
        return scrollView;
    }()
    
    

}
